package game.Sprites;

import game.Audio.AudioResources;
import game.Gamemode.GamemodeManager;
import game.Player.Player;
import game.Rendering.Camera;
import game.Rendering.PointLight;
import game.Scene;
import game.Utils.GraphicsUtils;
import game.Vector3;
import game.Weapons.Weapon_info;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.awt.geom.Rectangle2D;

public class PlantedBomb extends Sprite{
    double size = 1.0;
    long plantedTime;
    long explodeTime;

    long startTickTime = 2 * 1000L;
    long endTickTime = 100L;
    long nextTickTime;

    Image plantedSprite;

    public PlantedBomb(Vector3 position, double rotation, double secsToExplode) throws Exception {
        super(position, rotation, Color.WHITE);
        plantedTime = System.currentTimeMillis();
        explodeTime = plantedTime + (long)(secsToExplode*1000L);
        plantedSprite = SpriteResources.getPlantedBombImage();
        nextTickTime = System.currentTimeMillis();
    }

    @Override
    public void draw(GraphicsContext gd, Vector3 screenPos, double squareRes) {
        // Draw shadow
        GraphicsUtils.drawSpriteShadow(gd, screenPos, 1.0, 1.0, squareRes);

        // Draw bomb sprite
        GraphicsUtils.drawRotatedImage(gd, plantedSprite, rotation + Math.PI/2,
                (screenPos.x - squareRes*size / 2),
                (screenPos.y - squareRes*size / 2),
                squareRes*size, squareRes*size);

    }

    @Override
    public Rectangle2D.Double getBound() {
        return new Rectangle2D.Double(this.position.x - size/2, this.position.y - size/2, size, size);
    }

    protected long getCurrentTickTime(){
        double p = (double)(System.currentTimeMillis() - plantedTime) / (explodeTime - plantedTime);

        return  (long)((1.0-p) * startTickTime + p*endTickTime);
    }
    @Override
    public void update(double deltaTime) {
        if(System.currentTimeMillis() >= explodeTime){
            //Destroy? -> GamemodeManager should call explosion before this
            this.explode();
            //this.destroy();
        }

        if(System.currentTimeMillis() >= nextTickTime){
            // Set next tick time
            nextTickTime = System.currentTimeMillis() + getCurrentTickTime();

            // Make tick sound
            if(Scene.getInstance() != null && Scene.getInstance().getCamera() != null){
                Scene.getInstance().getCamera().play3DSound(AudioResources.getC4Sound(), this.position);
            }

            // Create light
            try {
                Vector3 lightPos = this.position;
                lightPos.z = 1.0;
                PointLight l = new PointLight(lightPos, 1.0, Color.RED);
                l.destroy(0.1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Will be called by the GameManager
     */
    public void explode(){
        // Explode bomb animation
        try {
            Vector3 explosionPos = this.position;
            explosionPos.z = 32;
            new Explosion(explosionPos, this.rotation, 10.0, Color.WHITE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Do damage to players
        double bombDist = 10;
        double killDist = 3;
        //Weapon_info bombInfo = new Weapon_info("bomb","bomb", "Bomb", 0, 0, 0,
        //        0,0,100,0,0,0,0,0,0,0);
        for(Player pl : GamemodeManager.getInstance().getPlayerList()){
            double plDist = Vector3.distance2d(pl.getPosition(), this.position);
            if(plDist < bombDist){
                double damage = 150;
                if(plDist > killDist){
                    damage = damage*(plDist-killDist)/(bombDist-killDist);
                }
                pl.doDamage(pl, null, (int)(damage+0.5));
            }
        }

        // Explode sound
        if(Scene.getInstance() != null && Scene.getInstance().getCamera() != null){
            Scene.getInstance().getCamera().play3DSound(AudioResources.getC4ExplodeSound(), this.position);
        }

        this.destroy();
    }
    /**
     * Will be called by the GameManager
     */
    public void defuse(){
        // Defuse bomb

        // Defuse sound
        if(Scene.getInstance() != null && Scene.getInstance().getCamera() != null){
            Scene.getInstance().getCamera().play3DSound(AudioResources.getC4DisarmSound(), this.position);
        }
        this.destroy();
    }
}
